namespace WenYu.Core.Lexer;

/// <summary>
/// Token数据结构
/// </summary>
public class Token
{
    /// <summary>Token类型</summary>
    public TokenType Type { get; set; }
    
    /// <summary>Token值（原始文本）</summary>
    public string Value { get; set; }
    
    /// <summary>行号（从1开始）</summary>
    public int Line { get; set; }
    
    /// <summary>列号（从1开始）</summary>
    public int Column { get; set; }
    
    /// <summary>在源代码中的起始位置（从0开始）</summary>
    public int Position { get; set; }
    
    /// <summary>
    /// 构造函数
    /// </summary>
    public Token(TokenType type, string value, int line, int column, int position)
    {
        Type = type;
        Value = value;
        Line = line;
        Column = column;
        Position = position;
    }
    
    /// <summary>
    /// 便捷构造函数（位置信息后设置）
    /// </summary>
    public Token(TokenType type, string value)
    {
        Type = type;
        Value = value;
        Line = 1;
        Column = 1;
        Position = 0;
    }
    
    /// <summary>
    /// 字符串表示
    /// </summary>
    public override string ToString()
    {
        return $"[{Type}] '{Value}' @ ({Line}:{Column})";
    }
    
    /// <summary>
    /// 判断是否为关键字
    /// </summary>
    public bool IsKeyword()
    {
        return Type == TokenType.Keyword || 
               Type == TokenType.TypeKeyword || 
               Type == TokenType.ArrayTypeKeyword;
    }
    
    /// <summary>
    /// 判断是否为运算符
    /// </summary>
    public bool IsOperator()
    {
        return Type == TokenType.Operator || 
               Type == TokenType.ComparisonOperator || 
               Type == TokenType.LogicalOperator;
    }
    
    /// <summary>
    /// 判断是否为字面量
    /// </summary>
    public bool IsLiteral()
    {
        return Type == TokenType.IntegerLiteral || 
               Type == TokenType.DoubleLiteral || 
               Type == TokenType.StringLiteral || 
               Type == TokenType.BooleanLiteral;
    }
}

